<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <title>图书管理系统</title>
    <link rel="stylesheet" href="../css/register.css">
    <script src="../js/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#password').focus(function () {
                // 密码框获得焦点，追加样式.password
                $('#owl').addClass('password');
            }).blur(function () {
                // 密码框失去焦点，移除样式.password
                $('#owl').removeClass('password');
            })
        });
        $(function () {
            $('#passwordagain').focus(function () {
                // 密码框获得焦点，追加样式.password
                $('#owl').addClass('password');
            }).blur(function () {
                // 密码框失去焦点，移除样式.password
                $('#owl').removeClass('password');
            })
        })
    </script>
</head>

<body>
<div class="regiester-msg" style="color: white">
    <h1>猫头鹰图书管理系统</h1>

</div>
<div class="login-box">
    <div class="owl" id="owl">
        <div class="hand"></div>
        <div class="hand hand-r"></div>
        <div class="arms">
            <div class="arm"></div>
            <div class="arm arm-r"></div>
        </div>
    </div>
    <div v-model="formData" id="app" class="input-box">
        <input id="username" type="text" v-model="formData.username" @blur="getusername" placeholder="用户名">
        <input id="Email" type="text" v-model="formData.email"  placeholder="邮箱">
        <input id="password" type="password" v-model="formData.password" placeholder="密码">
        <input id="passwordagain" type="password" v-model="formData.passwordagain" @blur="passwordVSpasswordagain" placeholder="确认密码">
        <p id="msg2" style="visibility: hidden">p</p>
        <button @click="saveUser()">注册</button>
    </div>
    <div class="regiesterbypassword" id="reg-password" style="visibility: hidden">
        <div style="text-align: center">
            <div style="color: red">密码输入不一致，请重新输入
            </div>
        </div>
    </div>
    <div class="regiesterbyusername" id="reg-username" style="visibility: hidden">
        <div style="text-align: center">
            <div style="color: red">用户名已存在
            </div>
        </div>
    </div>
    <div class="regiester"><a href="http://localhost/pages/log.html">立刻登录</a></div>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            usernamefalg:true,
            passwordfalg:true,
            formData: {
                username: '',
                email: '',
                password: '',
                passwordagain: ''
            },//表单数据
        },
        methods: {
            getusername(){
                axios.get("http://localhost/users/"+this.formData.username).then((res)=>{
                    if (res.data.code != 901) {
                        document.getElementById("reg-password").style.visibility="hidden";
                        document.getElementById("reg-username").style.visibility="visible";
                        setTimeout(function (){
                            document.getElementById("reg-username").style.visibility="hidden"
                        },3000)
                        this.usernamefalg=false;
                    }else if (res.data.code==901){
                        document.getElementById("reg-username").style.visibility="hidden";
                        this.usernamefalg=true
                    }
                });
            },
            passwordVSpasswordagain(){

                if (this.formData.password!=this.formData.passwordagain){
                    document.getElementById("reg-username").style.visibility="hidden";
                    document.getElementById("reg-password").style.visibility="visible";
                    setTimeout(function (){
                        document.getElementById("reg-password").style.visibility="hidden"
                    },3000)
                    this.passwordfalg=false
                }else if (this.formData.password==this.formData.passwordagain) {
                    document.getElementById("reg-password").style.visibility="hidden";
                    this.passwordfalg=true
                }
            },
            saveUser() {
                var vm=this;
                this.getusername();
                this.passwordVSpasswordagain();
                if (this.usernamefalg&&this.passwordfalg){
                    axios.post("http://localhost/users/1", this.formData).then((res) => {
                        if (res.data.code == 1001) {
                            document.location.href = "http://localhost/pages/log.html"
                        }else {
                            this.$message.error("系统繁忙，请稍后再试")
                        }
                    });
                }
            }
        }
    })
</script>
</html>

